from fastapi import APIRouter, Query,UploadFile
from tortoise.expressions import Q
from app.core.ctx import CTX_USER_ID
from app.models import User, Customers
from app.settings.config import settings
from app.controllers.answer.answer_list  import answer_controller
from app.schemas.base import Success, SuccessExtra,Fail
from app.schemas.answer import AnswerCreate, AnswerUpdate

answer_router = APIRouter()
model_name = "问答"


@answer_router.get("/list", summary=f"查看{model_name}列表")
async def do_list(
        page: int = Query(1, description="页码"),
        page_size: int = Query(100, description="每页数量"),
        question: str = Query("", description="标题"),
):
    q = Q()
    if question:
        q &= Q(question__contains=question)
    total, data = await answer_controller.do_list(page=page, page_size=page_size, search=q)
    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@answer_router.get("/get", summary=f"查看{model_name}")
async def do_get(
        data_id: int = Query(..., description="ID"),
):
    data = await answer_controller.do_detail(data_id=data_id)
    return Success(data=data)

@answer_router.post("/create", summary=f"创建{model_name}")
async def do_create(
        category_in: AnswerCreate,
):
    add_id = CTX_USER_ID.get()
    category_in.add_id = add_id
    await answer_controller.do_create(obj_in=category_in)
    return Success(msg="创建成功")

@answer_router.post("/update", summary=f"更新{model_name}")
async def do_update(
        category_in: AnswerUpdate,
):
    await answer_controller.do_update(obj_in=category_in)
    return Success(msg="更新成功")

@answer_router.delete("/delete", summary=f"删除{model_name}")
async def do_delete(
        data_id: int = Query(..., description="ID"),
):
    await answer_controller.do_delete(data_id=data_id)
    return Success(msg="删除成功")




